<?php
namespace app\admin\controller;

use app\common\controller\Admin;
use think\facade\View;

class Mood extends Admin
{
    protected $modelClass = '\app\common\model\Mood';
    protected $searchTime = 'a.create_time';
    
    protected function setLang()
    {
        $this->searchArr = [
            'title' => lang('title'),
            'typename' => lang('typeid'),
            'username' => lang('mid_text').lang('username'),
            'name' => lang('mid_text').lang('name'),
        ];
    }
    
    public function index()
    {
        $this->_indexSearch();
        
        $where = [];
        $getSearch = $this->_getSearch();
        if (!empty($getSearch)) $where[] = $getSearch;
        
        if (input('get._sort')){
            $order = explode(',', input('get._sort'));
            $order = $order[0].' '.$order[1];
        }else{
            $order = 'id desc';
        }
        $dataList = $this->cModel
        ->alias('a')
        ->join('arctype b', 'a.typeid = b.id', 'left')
        ->join('member c', 'a.mid = c.id', 'left')
        ->field('a.*,b.typename,b.mod,c.username,c.name')
        ->where($where)->order($order)->paginate('', false, page_param());
        
        View::assign('dataList', $dataList);
        return View::fetch();
    }
    
    public function edit()
    {
        if (request()->isPost()){
            $data = input('post.', '', 'htmlspecialchars');
            $id = $data['id'];
            if (count($data) == 2){
                foreach ($data as $k =>$v){
                    $fv = $k!='id' ? $k : '';
                }
                $validate = 'app\common\validate\\'.CONTROLLER_NAME;
                $validate = new $validate;
                if (!$validate->scene($fv)->check($data)) {
                    return ajax_return($validate->getError());
                }
                $result = $this->cModel->where('id', $id)->save($data);
            }else{
                $validate = 'app\common\validate\\'.CONTROLLER_NAME;
                $validate = new $validate;
                if (!$validate->scene('edit')->check($data)) {
                    return ajax_return($validate->getError());
                }
                $data['flag'] = $this->cModel->setFlagAttr($data['flag'], $data);
                $result = $this->cModel->where('id', $id)->save($data);
            }
            if ($result){
                return ajax_return(lang('action_success'), url('index'));
            }else{
                return ajax_return(lang('action_fail'));
            }
        }else{
            $id = input('get.id');
            $data = $this->cModel->find($id);
            View::assign('data', $data);
            return View::fetch();
        }
    }
}